package com.gewu.pm.ui.activity.home;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TestBean;
import com.gewu.pm.ui.adapter.home.ReportPersonAdapter;
import com.gewu.pm.widget.ProgressData;
import com.gewu.pm.widget.RingProgressViewFixed;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 日报报表
 */
public final class ReportDailyActivity extends AppActivity {

    private ImageView mDatePrev;
    private TextView mSelectedDate;
    private ImageView mDateNext;
    private RingProgressViewFixed mRingProgress;
    private TextView mSubmittedCount;
    private TextView mUnsubmittedCount;
    private RelativeLayout mSubmittedTitle;
    private RelativeLayout mUnsubmittedTitle;
    private ImageView mSubmittedArrow;
    private ImageView mUnsubmittedArrow;
    private RecyclerView mSubmittedRecyclerView;
    private RecyclerView mUnsubmittedRecyclerView;

    private Calendar mCurrentDate;
    private SimpleDateFormat mDateFormat;
    private SimpleDateFormat mYearDateFormat;

    private ReportPersonAdapter mSubmittedAdapter;
    private ReportPersonAdapter mUnsubmittedAdapter;

    private boolean mSubmittedExpanded = true;
    private boolean mUnsubmittedExpanded = true;

    public static void start(Context context) {
        Intent intent = new Intent(context, ReportDailyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_daily_report_statistics;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mDatePrev = findViewById(R.id.iv_date_prev);
        mSelectedDate = findViewById(R.id.tv_selected_date);
        mDateNext = findViewById(R.id.iv_date_next);
        mRingProgress = findViewById(R.id.ring_progress);
        mSubmittedCount = findViewById(R.id.tv_submitted_count);
        mUnsubmittedCount = findViewById(R.id.tv_unsubmitted_count);
        mSubmittedTitle = findViewById(R.id.tv_submitted_title);
        mUnsubmittedTitle = findViewById(R.id.tv_unsubmitted_title);
        mSubmittedArrow = findViewById(R.id.iv_submitted_arrow);
        mUnsubmittedArrow = findViewById(R.id.iv_unsubmitted_arrow);
        mSubmittedRecyclerView = findViewById(R.id.rv_submitted_list);
        mUnsubmittedRecyclerView = findViewById(R.id.rv_unsubmitted_list);

        // 初始化日期格式化器
        mDateFormat = new SimpleDateFormat("MM-dd E", Locale.getDefault());
        mYearDateFormat = new SimpleDateFormat("yyyy-MM-dd E", Locale.getDefault());

        // 初始化当前日期
        mCurrentDate = Calendar.getInstance();

        // 设置RecyclerView
        setupRecyclerViews();

        // 设置点击事件
        setOnClickListener(mDatePrev, mSelectedDate, mDateNext,
                mSubmittedTitle, mUnsubmittedTitle);
    }

    @Override
    protected void initData() {
        // 更新日期显示
        updateDateDisplay();

        // 加载数据
        loadReportData();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_date_prev) {
            // 日期减1天
            mCurrentDate.add(Calendar.DAY_OF_MONTH, -1);
            updateDateDisplay();
            loadReportData();
        } else if (id == R.id.tv_selected_date) {
            // 显示日期选择器
            showDatePicker();
        } else if (id == R.id.iv_date_next) {
            // 日期加1天
            mCurrentDate.add(Calendar.DAY_OF_MONTH, 1);
            updateDateDisplay();
            loadReportData();
        } else if (id == R.id.tv_submitted_title) {
            // 切换已交列表展开状态
            toggleSubmittedList();
        } else if (id == R.id.tv_unsubmitted_title) {
            // 切换未交列表展开状态
            toggleUnsubmittedList();
        }
    }

    /**
     * 设置RecyclerView
     * 已交和未交列表使用相同的adapter，根据UI设计图样式统一
     */
    private void setupRecyclerViews() {
        // 已交列表 - 使用统一的ReportPersonAdapter
        mSubmittedAdapter = new ReportPersonAdapter(this);
        mSubmittedRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mSubmittedRecyclerView.setAdapter(mSubmittedAdapter);

        // 未交列表 - 使用相同的ReportPersonAdapter
        mUnsubmittedAdapter = new ReportPersonAdapter(this);
        mUnsubmittedRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mUnsubmittedRecyclerView.setAdapter(mUnsubmittedAdapter);
    }

    /**
     * 显示日期选择器
     */
    private void showDatePicker() {
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                this,
                (view, year, month, dayOfMonth) -> {
                    mCurrentDate.set(year, month, dayOfMonth);
                    updateDateDisplay();
                    loadReportData();
                },
                mCurrentDate.get(Calendar.YEAR),
                mCurrentDate.get(Calendar.MONTH),
                mCurrentDate.get(Calendar.DAY_OF_MONTH)
        );
        datePickerDialog.show();
    }

    /**
     * 更新日期显示
     */
    private void updateDateDisplay() {
        Calendar today = Calendar.getInstance();
        boolean isCurrentYear = mCurrentDate.get(Calendar.YEAR) == today.get(Calendar.YEAR);

        String dateText;
        if (isCurrentYear) {
            // 今年显示：月-日 周几
            dateText = mDateFormat.format(mCurrentDate.getTime());
        } else {
            // 非今年显示：年-月-日 周几
            dateText = mYearDateFormat.format(mCurrentDate.getTime());
        }

        mSelectedDate.setText(dateText);
    }

    /**
     * 加载报表数据
     */
    private void loadReportData() {
        // 模拟数据
        int submittedCount = 8;
        int unsubmittedCount = 4;
        int totalCount = submittedCount + unsubmittedCount;

        // 创建多进度数据列表
        List<ProgressData> progressList = new ArrayList<>();
        progressList.add(new ProgressData(submittedCount, Color.parseColor("#C90000"), "已交"));
        progressList.add(new ProgressData(unsubmittedCount, Color.parseColor("#FF5500"), "未交"));

        // 更新圆环进度
        mRingProgress.setProgressList(progressList);

        // 设置中心文字
        int submittedPercentage = totalCount > 0 ? Math.round((float) submittedCount / totalCount * 100) : 0;
        mRingProgress.setText("已交", submittedPercentage + "%");

        // 更新统计数字
        mSubmittedCount.setText("已交（" + submittedCount + "）");
        mUnsubmittedCount.setText("未交（" + unsubmittedCount + "）");

        // 加载已交列表数据
        List<TestBean> submittedList = createSubmittedData();
        mSubmittedAdapter.setData(submittedList);

        // 加载未交列表数据
        List<TestBean> unsubmittedList = createUnsubmittedData();
        mUnsubmittedAdapter.setData(unsubmittedList);
    }

    /**
     * 创建已交数据
     */
    private List<TestBean> createSubmittedData() {
        List<TestBean> list = new ArrayList<>();
        String[] names = {"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"};

        for (int i = 0; i < names.length; i++) {
            TestBean bean = new TestBean(
                    "submitted_" + (i + 1),
                    names[i],
                    "submitted"
            );
            list.add(bean);
        }

        return list;
    }

    /**
     * 创建未交数据
     */
    private List<TestBean> createUnsubmittedData() {
        List<TestBean> list = new ArrayList<>();
        String[] names = {"陈十一", "林十二", "黄十三", "刘十四"};

        for (int i = 0; i < names.length; i++) {
            TestBean bean = new TestBean(
                    "unsubmitted_" + (i + 1),
                    names[i],
                    "unsubmitted"
            );
            list.add(bean);
        }

        return list;
    }

    /**
     * 切换已交列表展开状态
     */
    private void toggleSubmittedList() {
        mSubmittedExpanded = !mSubmittedExpanded;
        mSubmittedRecyclerView.setVisibility(mSubmittedExpanded ? View.VISIBLE : View.GONE);
        mSubmittedArrow.setRotation(mSubmittedExpanded ? 0f : -180f);
    }

    /**
     * 切换未交列表展开状态
     */
    private void toggleUnsubmittedList() {
        mUnsubmittedExpanded = !mUnsubmittedExpanded;
        mUnsubmittedRecyclerView.setVisibility(mUnsubmittedExpanded ? View.VISIBLE : View.GONE);
        mUnsubmittedArrow.setRotation(mUnsubmittedExpanded ? 0f : -180f);
    }
}
